/**
 * 
 */
package com.seguriapp.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.seguriapp.R;
import com.seguriapp.dao.TramitesDao;
import com.seguriapp.entities.TramiteEntity;

/**
 * @author installer
 * 
 */
public class TramitesAdapter extends SimpleCursorAdapter {

	// private Context mContext;

	@SuppressLint("NewApi")
	public TramitesAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		// mContext = context;
	}

	/**
	 * Obtiene el numero total de items a mostrar
	 */
	@Override
	public int getCount() {
		return getCursor().getCount();
	}

	/**
	 * Obtiene el view del nuevo item para mostrarlo en pantalla
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {

		final RelativeLayout viewRelative = (RelativeLayout) super.getView(
				position, convertView, parent);

		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);
		final TextView twTitle = (TextView) viewRelative
				.findViewById(R.id.entity_title);
		twTitle.setText(cursor.getString(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_NAME)));
		return viewRelative;
	}

	@Override
	public Object getItem(int position) {
		final Cursor cursor = getCursor();
		final TramiteEntity tramite = new TramiteEntity();
		tramite.setId(cursor.getLong(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_ID)));
		tramite.settId(cursor.getInt(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_TID)));
		tramite.setName(cursor.getString(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_NAME)));
		tramite.setCost(cursor.getString(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_COST)));
		tramite.setDuration(cursor.getString(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_DURATION)));
		tramite.setHasFile(cursor.getInt(cursor
				.getColumnIndexOrThrow(TramitesDao.COL_HAS_FILE)) > 0 ? true
				: false);
		return tramite;
	}

	@Override
	protected void onContentChanged() {
		super.onContentChanged();
		notifyDataSetChanged();
	}

}
